<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 头部 -->
        <el-row>
          <el-col :span="18">{{ companyName }}</el-col>
          <el-col :span="6">
            <el-row type="flex" justify="end">
              <el-col>负责人</el-col>
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down el-icon--right" /></span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDep('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="18">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="6">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDep(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDep(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDep(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

    </div>
    <el-dialog :title="isEdit?'编辑':'添加'" :visible.sync="showDialog">
      <AddorEdit v-if="showDialog" :visible.sync="showDialog" :pid="pid" :row="row" :is-edit="isEdit" @success="loadDep" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData, transArray, transArray2 } from '@/utils/index'
import AddorEdit from './deptDialog.vue'

export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [{
        name: '财务部',
        manager: '刘备',
        children: [{ name: '财务核算部', manager: '张飞' },
          { name: '税务核算部', manager: '关羽' }
        ]
      }],
      companyName: '',
      showDialog: false,
      pid: '',
      isEdit: false,
      row: {}
    }
  },
  created() {
    this.loadDep()
  },
  methods: {
    async loadDep() {
      const res = await getDepartments()
      // console.log(res.data.depts)
      if (res.code === 10000) {
        // 工具函数
        // this.list = tranListToTreeData(res.data.depts)
        // arrayToTree包
        // this.list = transArray(res.data.depts)
        // 递归
        this.list = transArray2(res.data.depts, '')
        // console.log(this.list)
        this.companyName = res.data.companyName
      }
    },
    addDep(pid) {
      this.isEdit = false
      this.showDialog = true
      this.pid = pid
    },
    delDep(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        // console.log(res)
        if (res.code !== 10000) return
        this.loadDep()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editDep(data) {
      this.isEdit = true
      this.showDialog = true
      this.row = data
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    padding: 30px 90px;
    .el-row{
      width: 100%;
    }
  }
</style>
